
// Program to take a TunerStudio .ini file and do the reformatting necessary to be the flash portion of variables.h

/*

Example input:

; comment
      man_crank_corr_variable = scalar,  U8,    544,     ... "Fixed axis","Variable axis"//      (1 byte) Table axis type?

Output:

#define man_crank_corr_variable  (*(CONST uint8_t *)(&Page_Ptr[1][544]))

*/

#include <stdio.h>
#include <string.h>

int main()
{
    char *ptr;
    char name[1024];
    char prev_name[1024];
    char type[1024];
    char size[1024];
    int offset = 0;
    int prev_offset;
    char line[1024];
    char line2[1024];
    int page = 0;
    int table = 0;              // currently working on a table
    int i;

    while (fgets(line, sizeof(line), stdin)) {

        if (strncmp(line, "[OutputC", 8) == 0)
            break;              // end of flash variables

        if (table) {            // processing a table - skip the elements
            if (strstr(line, "_Cal"))
                table = 0;
            continue;
        }

        i = sscanf(line, "%*[ ]%[^, =]%*[, =]%[^, ]%*[, ]%[^, ]%*[, ]%d%s",
                   name, type, size, &offset, line2);

        if (name[0] == ';' || name[0] == '\0') ;
        else if (i > 3) {

            if (strcmp(name, prev_name) != 0) {

                if (strcmp(type, "scalar") == 0 || strcmp(type, "bits") == 0) {
                    if (offset < prev_offset)
                        ++page;
                    prev_offset = offset;
                    ptr = strstr(name, "_Rows");        // detect a table
                    if (ptr) {
                        *ptr = 0;
                        printf
                            ("#define %s_Table ((CONST struct table_jz *)(&Page_Ptr[%d][%d]))\n",
                             name, page, offset);
                        table = 1;
                    } else {
                        ptr = strstr(name, "_1");       // detect an array that was declared as a series of scalars
                        if (ptr && *(ptr + 2) == '\0') {
                            printf
                                ("#define %s (*(CONST %s *)(&Page_Ptr[%d][%d]))\n",
                                 name, size, page, offset);
                            *ptr = 0;
                            printf
                                ("#define %s_Array ((CONST %s *)(&Page_Ptr[%d][%d]))\n",
                                 name, size, page, offset);
                        } else
                            printf
                                ("#define %s (*(CONST %s *)(&Page_Ptr[%d][%d]))\n",
                                 name, size, page, offset);
                    }
                }
                if (strcmp(type, "array") == 0) {
                    if (offset < prev_offset)
                        ++page;
                    prev_offset = offset;
                    //printf("// %s",line);
                    printf
                        ("#define %s_Array ((CONST %s *)(&Page_Ptr[%d][%d]))\n",
                         name, size, page, offset);
                }

            }
            strcpy(prev_name, name);

        }

    }                           // while

    return 0;

}                               // main
